package com.app.lxx.pregnancyberth.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;

import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.app.lxx.pregnancyberth.R;
import com.app.lxx.pregnancyberth.base.UrlManage;
import com.app.lxx.pregnancyberth.easyphone.EasyLinphone;
import com.app.lxx.pregnancyberth.utils.MLOC;
import com.app.lxx.pregnancyberth.widge.SharedPreferencesUtils;

import static com.app.lxx.pregnancyberth.utils.NetUtils.getIPAddress;

/**
 * Created by Lxx on PregnancyBerth
 * 2019/8/26 11:25 星期三
 * Details：首页 启动页
 */
public class MainActivity extends AppCompatActivity {

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        SharedPreferencesUtils sharedPreferencesUtils = new SharedPreferencesUtils(this);
        String serviceIp = sharedPreferencesUtils.getServiceIp();
        if (!TextUtils.isEmpty(serviceIp)) {
            UrlManage.imageUrl = "http://" + serviceIp;
            UrlManage.APP_URL = "http://" + serviceIp + "/api/";
            Toast.makeText(this, "服务器IP=>" + UrlManage.APP_URL, Toast.LENGTH_LONG).show();
        }

        MLOC.localIpAddress = getIPAddress(this); //获取本机ip地址

        //点击之后跳转到打电话。并将ip等信息传递过去
//        findViewById(R.id.call_phone).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                updateOnlineState(); //更新用户状态。
//                VoipP2PActivity.newInstance(MainActivity.this);
//            }
//        });

        String basePath = getFilesDir().getAbsolutePath();
        Log.e("basePath", basePath);
        // LinphoneService
        EasyLinphone.startService(this);
    }


    Runnable task;

    @Override
    protected void onResume() {
        super.onResume();
        if (task != null) {
            handler.removeCallbacks(task);
        }
        task = new Runnable() {
            @Override
            public void run() {
                if (checkIsFirstRun()) {
                    Intent intent = new Intent(getApplicationContext(), ControllerActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    VoipP2PActivity.newInstance(MainActivity.this);
                }
            }
        };
        handler.postDelayed(task, 3000);

    }

    //检测是否为第一次运行
    public boolean checkIsFirstRun() {
        SharedPreferences preferences = getSharedPreferences("isFirstRun", MODE_PRIVATE);
        boolean isFirstRun = preferences.getBoolean("key", true);
        if (isFirstRun) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("key", false);
            editor.apply();
        }
        return isFirstRun;
    }

//     更新上线状态
//    public void updateOnlineState() {
//        String username = UserNameUtil.getUsername(this);
//        HttpUtil.sendOkHttpRequest(MLOC.baseURl + "server/stateServlet1?username=" + username +
//                "&ip=" + getIPAddress(MainActivity.this), new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Toast.makeText(MainActivity.this, "上线成功", Toast.LENGTH_SHORT).show();
//                    }
//                });
//            }
//        });
//    }
}
